//
//  CommonTool.m
//  礼管家
//
//  Created by tim on 16/3/5.
//  Copyright © 2016年 tim. All rights reserved.
//

#import "CommonTool.h"

@implementation CommonTool

#pragma mark - 验证银行卡号
+ (BOOL) checkCardNo:(NSString*) cardNo{
    int oddsum = 0;    //奇数求和
    int evensum = 0;    //偶数求和
    int allsum = 0;
    int cardNoLength = (int)[cardNo length];
    int lastNum = 0;
    if (cardNoLength>0) {
        lastNum = [[cardNo substringFromIndex:cardNoLength-1] intValue];
        cardNo = [cardNo substringToIndex:cardNoLength -1];
    }

    for (int i = cardNoLength -1 ; i>=1;i--) {
        NSString *tmpString = [cardNo substringWithRange:NSMakeRange(i-1,1)];
        int tmpVal = [tmpString intValue];
        if (cardNoLength % 2 ==1 ) {
            if((i % 2) == 0){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }else{
            if((i % 2) == 1){
                tmpVal *= 2;
                if(tmpVal>=10)
                    tmpVal -= 9;
                evensum += tmpVal;
            }else{
                oddsum += tmpVal;
            }
        }
    }
    
    allsum = oddsum + evensum;
    allsum += lastNum;
    if((allsum % 10) ==0)
        return YES;
    else
        return NO;
}




#pragma mark - 验证电话号码
+(BOOL)isValidateTelNumber:(NSString *)number {
    
    NSString *strRegex = @"[0-9]{11,11}";
    
    BOOL rt = [self isValidateRegularExpression:number byExpression:strRegex];
    
    return rt;
    
}

#pragma mark - 是否是有效的正则表达式

+(BOOL)isValidateRegularExpression:(NSString *)strDestination byExpression:(NSString *)strExpression

{
    
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", strExpression];
    
    return [predicate evaluateWithObject:strDestination];
    
}
#pragma mark - 正则判断邮箱格式
+(BOOL)isValidateEmail:(NSString *)Email
{
    NSString *emailCheck = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",emailCheck];
    return [emailTest evaluateWithObject:Email];
}
#pragma mark - 正则判断手机号码地址格式
+ (BOOL)isMobileNumber:(NSString *)mobileNum{
    /**
     * 手机号码:
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     * 联通号段: 130,131,132,155,156,185,186,145,176,1709
     * 电信号段: 133,153,180,181,189,177,1700
     */
    //    NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
    NSString *MOBILE = @"^1((3[0-9]|4[57]|5[0-35-9]|7[0678]|8[0-9])\\d{8}$)";
    /**
     * 中国移动：China Mobile
     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     */
    NSString *CM = @"(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
    /**
     * 中国联通：China Unicom
     * 130,131,132,155,156,185,186,145,176,1709
     */
    NSString *CU = @"(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
    /**
     * 中国电信：China Telecom
     * 133,153,180,181,189,177,1700
     */
    NSString *CT = @"(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
    
    
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    NSPredicate *regextestcm = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CM];
    NSPredicate *regextestcu = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CU];
    NSPredicate *regextestct = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", CT];
    
    if (([regextestmobile evaluateWithObject:mobileNum] == YES)
        || ([regextestcm evaluateWithObject:mobileNum] == YES)
        || ([regextestct evaluateWithObject:mobileNum] == YES)
        || ([regextestcu evaluateWithObject:mobileNum] == YES))
    {
        if([regextestcm evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Mobile");
        } else if([regextestct evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Telecom");
        } else if ([regextestcu evaluateWithObject:mobileNum] == YES) {
            NSLog(@"China Unicom");
        } else {
            NSLog(@"Unknow");
        }
        
        return YES;
    }
    else
    {
        if (mobileNum.length<11) {
            return NO;
        }
        return NO;
    }

}

#pragma mark - 判断是否登录
+ (BOOL)IsUserLogin:(NSString *)userId
{
    
    if (userId != nil || [userId intValue] > 0)
    {
        //已经登录
        return YES;
        
    }  
    
    NSLog(@"未登录");
    return NO;  
    
}

//+ (double)getTimeStamp {
//    
//    NSTimeInterval diffTime = [[NSUserDefaults standardUserDefaults] doubleForKey:WM_SERVER_TIME_KEY];
//    
//    if (diffTime == NSNotFound) {
//        HTTPHelper *httpTool = [HTTPHelper sharedHTTPHelper];
//        [httpTool getTimeStamp];
//    }
//    
//    diffTime = [[NSUserDefaults standardUserDefaults] doubleForKey:WM_SERVER_TIME_KEY];
//    NSDate *date = [NSDate date];
//    NSTimeInterval timeStamp = [date timeIntervalSince1970];
//    NSTimeInterval realTime = timeStamp * 1000 + diffTime;
//    
//    NSLog(@"计算的时间戳%@",@(realTime));
//    
//    return realTime;
//}


+(NSString*)judgeCurrentdateWithDate:(double)beTime withType:(DateType)dateType{
    
    NSTimeInterval now = [[NSDate date]timeIntervalSince1970];
    double distanceTime = now - beTime;
    NSString * distanceStr;
    
    NSDate * beDate = [NSDate dateWithTimeIntervalSince1970:beTime];
    NSDateFormatter * df = [[NSDateFormatter alloc]init];
    [df setDateFormat:@"HH:mm"];
    NSString * timeStr = [df stringFromDate:beDate];
    
    [df setDateFormat:@"dd"];
    NSString * nowDay = [df stringFromDate:[NSDate date]];
    NSString * lastDay = [df stringFromDate:beDate];
    
    if(distanceTime <24*60*60 && [nowDay integerValue] == [lastDay integerValue]){//时间小于一天
        distanceStr = [NSString stringWithFormat:@"今天"];
    }
    else if(distanceTime<24*60*60*2 && [nowDay integerValue] != [lastDay integerValue]){
        
        distanceStr = [NSString stringWithFormat:@"昨天"];
 
    } else if(distanceTime <24*60*60*365){
        [df setDateFormat:@"yyyy.MM.dd"];
        distanceStr = [df stringFromDate:beDate];
    }
    
    
    if (dateType == kDateTypeHourMin) {
        distanceStr = [NSString stringWithFormat:@"%@",timeStr];
    }else if(dateType == kDateTypeFull) {
        [df setDateFormat:@"yyyy.MM.dd HH:mm"];
        distanceStr = [df stringFromDate:beDate];;
    }else if (dateType == kDateTypeMainFull) {
        [df setDateFormat:@"yyyy.MM.dd HH:mm:ss"];
        distanceStr = [df stringFromDate:beDate];;
    }
    return distanceStr;
}


+ (NSMutableArray *)getSameArrayList:(NSArray *)arrayList res:(NSMutableArray *)res key:(NSString *)key{

    
    NSMutableArray *diffArray = [NSMutableArray array];
    NSMutableArray *sameArray = [NSMutableArray array];
    [sameArray addObject:arrayList[0]];
    for (int i = 1; i < arrayList.count; i ++) {
        
        NSObject *model = arrayList[i];
        NSObject *sameModel = sameArray[0];
        if ([[CommonTool judgeCurrentdateWithDate:[[model valueForKey:key] doubleValue] withType:kDateTypeYearMonth] isEqualToString:[CommonTool judgeCurrentdateWithDate:[[sameModel valueForKey:key] doubleValue] withType:kDateTypeYearMonth]]) {
            [sameArray addObject:arrayList[i]];
        }else{
            [diffArray addObject:arrayList[i]];
        }
    }
    [res addObject:sameArray];
    if (diffArray.count != 0) {
        [CommonTool getSameArrayList:diffArray res:res key:key];
    }
    return res;
}
#pragma mark- 缓存到本地


//+ (void)saveCache:(int)type andID:(int)_id andString:(NSString *)str
//{
//       NSUserDefaults * setting = [NSUserDefaults standardUserDefaults];
//       NSString * key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
//       [setting setObject:str forKey:key];
//       [setting synchronize];
//}
//
//+ (NSString *)getCache:(int)type andID:(int)_id
//{
//        NSUserDefaults * settings = [NSUserDefaults standardUserDefaults];
//        NSString *key = [NSString stringWithFormat:@"detail-%d-%d",type, _id];
//    
//        NSString *value = [settings objectForKey:key];
//        return value;
//}


@end
